package com.bole.shiro;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    /**
     * ShiroFilterFactoryBean
     */
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(
            @Autowired DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        // 添加shiro内置过滤器
        /**
         * anon : 无需认证即可访问
         * authc ：必须认证才能访问
         * perms ：有权限才能访问
         * role ： 有角色权限才能访问
         */
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/css/**", "anon");
        filterMap.put("/js/**", "anon");
        filterMap.put("/font/**", "anon");
        filterMap.put("/images/**", "anon");
        filterMap.put("/layer/**", "anon");
        filterMap.put("/login", "anon");

        filterMap.put("/**", "authc");
//        filterMap.put("/order/**", "authc");
        // 退出登录固定写法
        filterMap.put("/auth/logout","logout");

        // 修改登录页面
        shiroFilterFactoryBean.setLoginUrl("/toLogin");
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");
//        shiroFilterFactoryBean.setSuccessUrl("/");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        return shiroFilterFactoryBean;
    }

    /**
     * 安全管理器
     */
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Autowired UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    @Bean
    public HashedCredentialsMatcher getHashedCredentialsMatcher(){
        //引入加密工具类HashedCredentialsMatcher：
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        return hashedCredentialsMatcher;
    }

    /**
     * 创建userRealm
     *
     * @return
     */
    @Bean
    public UserRealm getRealm(@Autowired HashedCredentialsMatcher hashedCredentialsMatcher) {
        UserRealm userRealm = new UserRealm();
        //设置我们要采用的加密方法的名称：
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
        //设置加密的次数：
        hashedCredentialsMatcher.setHashIterations(3);
        hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
        userRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return userRealm;
    }

//    @Bean
//    public ShiroDialect getShiroDialect(){
//        return new ShiroDialect();
//    }
}
